package com.arvato.wxwork.controller.admin.kpi;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.kpi.WxworkKpiItemDo;
import com.arvato.wxwork.bean.bean_vo.kpi.WxworkKpiItemVo;
import com.arvato.wxwork.service.kpi.WxworkKpiItemService;


/**
 * 企业微信KPI管理Controller
 * @author zhan655
 *
 */
@Controller
@RequestMapping("/admin/wxwork/kpi/item")
public class WxworkKpiItemController {
	
	private Logger logger = LoggerFactory.getLogger(WxworkKpiItemController.class);

	@Autowired
	private WxworkKpiItemService wxworkKpiItemService;
	
	/**
	 * 获取kpi列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public PageData listKpiItem(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		logger.info("获取kpi列表");
		PageData pageData = wxworkKpiItemService.listKpiItemByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 获取一个kpi的详情
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/detail")
	public Result KpiItemDetail(String id) {
		WxworkKpiItemVo KpiItemVo = wxworkKpiItemService.getKpiItemById(id);
		return Result.success(KpiItemVo);
	}
	
	/**
	 * 创建kpi
	 * @param req
	 * @param KpiItemDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert")
	public Result create(HttpServletRequest req, WxworkKpiItemDo KpiItemDo) {
		logger.info("新增一个kpi");
		try {
			KpiItemDo.setCreateTime(new Date());
			int row = wxworkKpiItemService.insert(KpiItemDo);
			if(row == 1) {
				return Result.success("创建kpi成功。");
			}
		} catch(DuplicateKeyException e) {
			return Result.fail("新增失败,kpi编号:" + KpiItemDo.getKpiCode() + "已存在");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("创建kpi失败：" + e.getMessage());
		}
		return Result.success("创建kpi成功。");
	}
	
	/**
	 * 更新kpi
	 * @param req
	 * @param KpiItemDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update")
	public Result update(HttpServletRequest req, WxworkKpiItemDo KpiItemDo) {
		logger.info("更新一个kpi");
		try {
			int row = wxworkKpiItemService.update(KpiItemDo);
			if(row == 1) {
				return Result.success("更新kpi成功。");
			}
		} catch(DuplicateKeyException e) {
			return Result.fail("新增失败,kpi编号:" + KpiItemDo.getKpiCode() + "已存在");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("更新kpi失败：" + e.getMessage());
		}
		return Result.success("更新kpi成功。");
	}
	
	/**
	 * 删除kpi
	 * @param req
	 * @param KpiItemId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public Result delete(HttpServletRequest req, String id) {
		logger.info("删除一个kpi项");
		try {
			int row = wxworkKpiItemService.delete(id);
			if(row == 1) {
				return Result.success("删除kpi成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("删除kpi失败：" + e.getMessage());
		}
		return Result.success("删除kpi成功。");
	}
	
}

